package com.jindun.paper.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jindun.paper.dao.BagOrderItemRepository;
import com.jindun.paper.dao.CheckedValueRepository;
import com.jindun.paper.dao.SettingRepository;
import com.jindun.paper.model.BagOrderItem;
import com.jindun.paper.model.CheckedValue;
import com.jindun.paper.model.Order;
import com.jindun.paper.service.ExclusionCalculator;
import com.jindun.paper.ui.bag.bean.BagOrderItemInputBean;
import com.jindun.paper.ui.bag.bean.BagOrderItemResultBean;
import com.jindun.paper.ui.bag.bean.BagOrderItemRowData;

@Component
public class BagOrderItemProcess {

	

	@Autowired
	private BagOrderItemRepository bagOrderItemRepository;
	
	@Autowired
	private CheckedValueRepository checkedValueRepo;

	

	@Autowired
	private ExclusionCalculator exclustionCalculator;



	@Autowired
	protected SettingRepository settingRepository;
	 
	
	public BagOrderItemInputBean loadItemInputBean(BagOrderItem bagOrderItem) {
		BagOrderItemInputBean bean = new BagOrderItemInputBean();
		bean.setAdjustExclusion(bagOrderItem.isManuallyAdjust());
		bean.setAdjustExclusionQty(bagOrderItem.getExclustionStubQty());
		bean.setAllowancePrice(bagOrderItem.getAllowancePrice());

		List<CheckedValue> bagCheckedValueList = this.checkedValueRepo
				.findByBagOrderItemAndFlagOrderById(bagOrderItem,
						CheckedValue.FLAG_BAG_COUNT);
		List<Double> bagCountList = new ArrayList<Double>();
		for (CheckedValue checkedValue : bagCheckedValueList) {
			bagCountList.add(checkedValue.getQty());
		}
		bean.setCheckedBagQtyList(bagCountList);

		List<CheckedValue> stubCheckedValueList = this.checkedValueRepo
				.findByBagOrderItemAndFlagOrderById(bagOrderItem,
						CheckedValue.FLAG_STUB_COUNT);
		List<Double> stubCountList = new ArrayList<Double>();
		for (CheckedValue checkedValue : stubCheckedValueList) {
			stubCountList.add(checkedValue.getQty());
		}
		bean.setStubQtyList(stubCountList);

		bean.setClientReportAvgBagQtyPerStub(bagOrderItem
				.getReportedAvgPerStub());
		bean.setClientReportStubQty(bagOrderItem.getReportedStubQty());
		bean.setLayer(bagOrderItem.getLayer());
		bean.setOtherAmount(bagOrderItem.getAdditionalAmount());
		bean.setPrice(bagOrderItem.getPrice());
		bean.setSingularBagQty(bagOrderItem.getAdditionalBagQty());
		bean.setStubSpec(bagOrderItem.getStubSpec());
		

		return bean;
	}

//	public BagOrderItemResultBean loadItemResultBean(
//			BagOrderItemInputBean inputBean, BagOrderItem bagOrderItem) {
//		return this.calcuator.calcByManualExclusion(inputBean,
//				bagOrderItem.getExclustionStubQty());
//	}
//	
	public void saveItems(Order bagOrder, List<BagOrderItemRowData> rowDatas) {
		for (BagOrderItemRowData row : rowDatas) {
			BagOrderItem item = new BagOrderItem();
			item.setAdditionalAmount(row.getInput().getOtherAmount());
			item.setAdditionalBagQty(row.getInput().getSingularBagQty());
			item.setAllowancePrice(row.getInput().getAllowancePrice());
			item.setBagOrder(bagOrder);
			
		
			Long value =  Math.round(row.getResult().getExactStubQty());
			item.setExactStubQty(value.intValue());
			item.setExclustionStubQty(row.getResult().getExclusionStubQty());
			item.setLayer(row.getInput().getLayer());
			item.setPrice(row.getInput().getPrice());
			item.setReportedAvgPerStub(row.getInput()
					.getClientReportAvgBagQtyPerStub());
			item.setReportedStubQty(row.getInput().getClientReportStubQty());
			item.setStubSpec(row.getInput().getStubSpec());
			item.setExcludedAvgPerStub(row.getResult().getAfterExclusionAvg());
			item.setManuallyAdjust(row.getInput().isAdjustExclusion());
			this.bagOrderItemRepository.save(item);
			this.saveCheckValues(item, row.getInput().getCheckedBagQtyList(),
					CheckedValue.FLAG_BAG_COUNT);
			this.saveCheckValues(item, row.getInput().getStubQtyList(),
					CheckedValue.FLAG_STUB_COUNT);
		}
	}
	
	private void saveCheckValues(BagOrderItem orderItem, List<Double> values,
			int valueType) {
		for (Double value : values) {
			CheckedValue checkValue = new CheckedValue();
			checkValue.setBagOrderItem(orderItem);
			checkValue.setFlag(valueType);
			checkValue.setQty(value);
			checkedValueRepo.save(checkValue);
		}

	}
	
	public void clearItems(Order bagOrder) {
		List<BagOrderItem> items = this.bagOrderItemRepository
				.findByBagOrderOrderById(bagOrder);
		for (BagOrderItem item : items) {
			List<CheckedValue> values = this.checkedValueRepo
					.findByBagOrderItemAndFlagOrderById(item,
							CheckedValue.FLAG_BAG_COUNT);
			values.addAll(this.checkedValueRepo
					.findByBagOrderItemAndFlagOrderById(item,
							CheckedValue.FLAG_STUB_COUNT));
			this.checkedValueRepo.delete(values);
		}

		this.bagOrderItemRepository.delete(items);

	}
	
	public List<BagOrderItemRowData> loadItems(Order bagOrder) {
		List<BagOrderItemRowData> list = new ArrayList<BagOrderItemRowData>();
		List<BagOrderItem> bagOrderItems = bagOrderItemRepository
				.findByBagOrderOrderById(bagOrder);
		
		List<BagOrderItemInputBean> rows = new ArrayList<BagOrderItemInputBean>();
		for (BagOrderItem bagOrderItem : bagOrderItems) {
			BagOrderItemInputBean input = this.loadItemInputBean(bagOrderItem);
			rows.add(input);
//			BagOrderItemResultBean result = this.loadItemResultBean(input,
//					bagOrderItem);
//			BagOrderItemRowData rowData = new BagOrderItemRowData(input, result);
//			list.add(rowData);
		}
		return this.exclustionCalculator.calc(rows);
		
	}

}
